using System;
using System.Collections.Generic;
using System.Text;


using System.IO;

namespace ConsoleCommandProcessor
{
    public static class CommandLine
    {
        public struct Arg
        {
            public string Name;
            public string Value;
        }

        public struct AvailableArgs
        {
            public const string Invalid = "invalid";
            public const string Path = "path";
            public const string Help = "help";
            public const string HelpShort = "?";
            public const string Output = "output";

        }

        public static bool IsAgrNameValid(string argName)
        {
            return (argName == AvailableArgs.Invalid ||
                    argName == AvailableArgs.Path ||
                    argName == AvailableArgs.Help ||
                    argName == AvailableArgs.HelpShort ||
                    argName == AvailableArgs.Output);
        }


        /// <summary>
        /// The format of parameter is "-NameOfParameter=ValueOfParameter"
        /// </summary>
        /// <param name="argString"></param>
        /// <returns></returns>
        public static Arg ParseArg(string argString)
        {
            Arg arg = new Arg();
            char[] separator ={ '=' };
            string[] parts = argString.Split(separator, 2);
            if (parts.Length > 0)
            {
                // name of parameter
                string name = parts[0];
                if (name.StartsWith("-"))
                {
                    name = name.Substring(1, name.Length - 1);
                }

                if (IsAgrNameValid(name))
                {
                    arg.Name = name;
                    arg.Value = (parts.Length == 2 ? parts[1] : "");
                }
                else
                {
                    //Invalid argument
                    arg.Name = AvailableArgs.Invalid;
                    arg.Value = "Unrecognized argument:" + argString[0];
                }
            }
            return arg;
        }


        // Calling example
        private static bool IsPathArgumentValid(string pathValue)
        {
           
            if (File.Exists(pathValue))
            {
                return true;
            }
            return false;
        }


        public static string FixPath(string path)
        {
            if(!path.EndsWith("\\"))
            {
                path=path+"\\";
            }
            return path;
        }

        

    }
}
